import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_image_compress/flutter_image_compress.dart';
import 'package:my_app/app/core/utils/file_util.dart';
// ignore: depend_on_referenced_packages
import 'package:path_provider/path_provider.dart' as path_provider;
import 'dart:ui' as ui;

/// image_util.dart
///
/// Created by fuchuanwei on Mon Mar 27 2023.
///
/// Description: 图片工具类 用于图片压缩、 添加水印等操作
///   flutter_image_compress
///

Future<File?> compressImageFileAndGetFile(
  File file, {
  String? targetPath,
  CompressFormat format = CompressFormat.webp,
  int quality = 90,
  int rotate = 0,
}) async {
  final dir = await path_provider.getTemporaryDirectory();
  FileInfo fileInfo = getFileInfoByFile(file);
  targetPath = targetPath ??
      '${dir.absolute.path}/${fileInfo.nameWithoutExtension}_compress${_getFileSuffixByCompressFormat(format)}';
  var result = await FlutterImageCompress.compressAndGetFile(
    file.path,
    targetPath,
    quality: quality,
    rotate: rotate,
    format: format,
  );
  return result;
}

///根据压缩类型获取文件后缀
String _getFileSuffixByCompressFormat(CompressFormat type) {
  if (type == CompressFormat.jpeg) {
    return '.jpg';
  } else if (type == CompressFormat.png) {
    return '.png';
  } else if (type == CompressFormat.heic) {
    return '.heic';
  } else {
    return '.webp';
  }
}

///文件类型图片添加水印
Future<File> fileTypeImageWatermark(
  File file,
  String watermark, {
  TextAlign? textAlign,
  Color? color,
}) async {
  final ui.PictureRecorder recorder = ui.PictureRecorder();
  final Canvas canvas = Canvas(recorder);

  ui.Image image =
      (await (await ui.instantiateImageCodec(file.readAsBytesSync()))
              .getNextFrame())
          .image;
  double imgWidth = image.width.toDouble(); //图片宽度
  double imgHeight = image.height.toDouble(); //图片高度

  final paint = Paint()
    ..color = Colors.white
    ..blendMode = BlendMode.screen
    ..strokeWidth = 3
    ..style = PaintingStyle.fill;
  final rect = Rect.fromLTWH(0, 0, imgWidth, imgHeight);
  canvas.drawImageRect(image, rect, rect, paint);
  final double fontSize = imgWidth * 0.03;
  final paragraphBuilder = ui.ParagraphBuilder(
      ui.ParagraphStyle(textAlign: TextAlign.right, fontSize: fontSize))
    ..pushStyle(ui.TextStyle(color: Colors.red, shadows: [
      Shadow(color: Colors.black, offset: Offset(0.5, 0.5), blurRadius: 1)
    ]))
    ..addText(watermark);

  final paragraph = paragraphBuilder.build();
  paragraph.layout(ui.ParagraphConstraints(width: rect.width));
  canvas.drawParagraph(paragraph,
      Offset(rect.width - paragraph.width - 10, imgHeight - fontSize * 2));
  final picture = recorder.endRecording();

  image = await picture.toImage(image.width, image.height);
  final bytes = (await image.toByteData(format: ui.ImageByteFormat.png))
      ?.buffer
      .asUint8List()
      .toList();
  if (bytes != null) {
    final dir = await path_provider.getTemporaryDirectory();
    FileInfo fileInfo = getFileInfoByFile(file);
    final watermarkfile =
        File('${dir.path}/${fileInfo.nameWithoutExtension}_$watermark.png');
    watermarkfile.writeAsBytesSync(bytes.toList());
    return watermarkfile;
  }
  return file;
}
